import request from "../sevices/request";

class Wish {
  static getwList = (userid: string) => request.get('/wishList', { params: { userid } })
  static getWishItem = (id:number) => request.get('/wishList',{ params: { id } })
  static addwList = async (obj: any) => {
    let userid = obj.id
    let data = obj.children
    let title = obj.title
    let res = await request.post('/wishList', { userid, title, children: [data],date:"",people:"" })
    if (res.status === 201) {
      return { data: { message: "添加成功" }, code: 200 }
    } else {
      return { data: { message: "添加失败" }, code: 111 }
    }
  }
  static addwListChild = async (obj: any) => {
    let { id, data } = obj
    let res = await request.patch("/wishList/" + id, { children: data })
    if (res.status === 200) {
      return { data: { message: "修改成功" }, code: 200 }
    } else {
      return { data: { message: "修改失败" }, code: 111 }
    }

  }
  static removewListChild = async (data: any) => {
    let { userid, proid } = data
    let res = await request.get("/wishList", { params: { userid } })
    if (res.status === 200) {
      let id: any = 0
      res.data.map((item: any) => {
        item.children.map((itm: any) => {
          if (itm.id === proid) {
            id = item.id
          }
        })
      })
      let newChild = res.data.filter((item:any)=> item.id === id)[0].children.filter((itm:any)=> itm.id !== proid)
      let res2 = await request.patch("/wishList/" + id, { children: newChild })
      if (res2.status === 200) {
        return { data: { data: res2.data, message: "取消收藏成功" }, code: 200 }
      } else {
        return { data: { message: "取消收藏失败" }, code: 111 }
      }
    }
  }
  static delwList = (id: string) => {
    request.delete("/wishList/" + id)
  }
  static updatewListName = async (data: any) => {
    let { name, id } = data
    let res = await request.patch("/wishList/" + id, { title: name })

    if (res.status === 200) {
      return { data: { data: res.data, message: "修改成功" }, code: 200 }
    } else {
      return { data: { message: "修改失败" }, code: 111 }
    }
  }
  static updatewListDateAndPeople = async (data: any) => {
    let { date, id ,people} = data
    let res = await request.patch("/wishList/" + id, { date,people })
    if (res.status === 200) {
      return { data: { data: res.data, message: "修改成功" }, code: 200 }
    } else {
      return { data: { message: "修改失败" }, code: 111 }
    }
  }
}

export default Wish